package xm.manager
{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	
	/**
	 * <br>설명 : </br> 
	 * @author winnie
	 */	
	public class ToolTipManager
	{
		
		private static var _instance : ToolTipManager;

		public static function get instance():ToolTipManager
		{
			if( _instance == null ) _instance = new ToolTipManager( new SingletonEnforcer() );
			return _instance;
		}
		
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		/**
		 *  paddingTop
		 */		
		public var paddingTop : Number = 15;
		
		/**
		 *  paddingRight
		 */		
		public var paddingRight : Number = 15;
		
		/**
		 *  paddingBottom
		 */		
		public var paddingBottom : Number = 15;
		
		/**
		 * paddingLeft 
		 */		
		public var paddingLeft : Number = 15;
		
		//-----------------------------------------------------------------------------------
		// protected vars
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _tooltipField : TextField;
		private var _skinPointer : MovieClip;
		private var _skinBg : MovieClip;
		
		
		//-----------------------------------------------------------------------------------
		// constructor
		//-----------------------------------------------------------------------------------
		/**
		 * ToolTipManager
		 * <br />
		 * <br />
		 * 
		 * @param se
		 */		
		public function ToolTipManager( se : SingletonEnforcer )
		{
			if( se == null ) throw new Error( "생성하여 사용할 수 없는 객체입니다." );
			
			var s : Stage = StageManager.getInstance().stage;
			
			_skinPointer = new XM_DEFAULT_SKIN_TOOLTIP_ARROW();
			_skinBg = new XM_DEFAULT_SKIN_TOOLTIP_BOX();
			
		}
		
		
		//-----------------------------------------------------------------------------------
		// public function
		//-----------------------------------------------------------------------------------
		
		/**
		 * @param msg : String - 툴팁 메세지
		 */		
		public function showTooltip( msg:String ):void
		{
			var s : Stage = StageManager.getInstance().stage;
			
			tooltipField.text = msg;
			tooltipField.width = tooltipField.textWidth + 4;
			tooltipField.height = tooltipField.textHeight + 4;
			
			if( s )
			{
				s.addChild( _skinPointer );
				s.addChild( _skinBg );
				s.addChild( tooltipField );
				setTooltipPosition();
			}
		}
		
		/**
		 * 툴팁 가리기.
		 */		
		public function hideTooltip():void
		{
			var s : Stage = StageManager.getInstance().stage;
			
			if( s && s.contains( tooltipField ) )
			{
				if( tooltipField.hitTestPoint( s.mouseX, s.mouseY, true ) == true )
				{
					tooltipField.addEventListener(MouseEvent.ROLL_OUT, onToolTipOut );
				}
				else
				{
					removeToolTip();
				}
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		/**
		 * @return : TextField - 툴팁텍스트필드 반환 
		 */		
		public function get tooltipField():TextField
		{
			if( _tooltipField == null )
			{
				_tooltipField = new TextField();
				_tooltipField.wordWrap = true;
				_tooltipField.multiline = true;
				
				var tf : TextFormat = new TextFormat( "돋움", 18, 0x4d6be4, true);
				tf.leading = 5;
				_tooltipField.setTextFormat( tf );
				_tooltipField.defaultTextFormat = tf;
			}
			
			return _tooltipField;
		}
		
		/**
		 * @param value : TextField - 툴팁으로 사용하고자 하는 텍스트필드 삽입.
		 */		
		public function set tooltipField(value:TextField):void
		{
			_tooltipField = value;
		}

		
		
		/**
		 * <br>패딩값 default : 0,0,0,0</br>
		 * <br> - 시계방향으로 위, 오른쪽, 아래, 왼쪽 순서임 </br> 
		 */
		public function get padding():Array
		{
			return [paddingTop, paddingRight, paddingBottom, paddingLeft];
		}
		
		/**
		 * @private : Array - 상, 우, 하, 좌 ( 시게방향 )
		 */
		public function set padding(value:Array):void
		{
			if( value == null ) value = [0,0,0,0];
			
			paddingTop = value[ 0 ];
			paddingRight = value[ 1 ];
			paddingBottom = value[ 2 ];
			paddingLeft = value[ 3 ];
		}
		

		//-----------------------------------------------------------------------------------
		// protected function
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private function
		//-----------------------------------------------------------------------------------
		
		// 툴팁 포지셔닝 
		// 모바일에서 툴팁 배경은 무조건 stageWidth 에 준한다. ( -padding )
		private function setTooltipPosition():void
		{
			var s : Stage = StageManager.getInstance().stage,
				sum : Number;
			
			_skinPointer.x = s.mouseX - _skinPointer.width/2;
			_skinPointer.y = s.mouseY;
			
			_skinBg.x = paddingLeft;
			_skinBg.y = _skinPointer.y + _skinPointer.height;
			
			tooltipField.x = _skinBg.x + paddingLeft;
			tooltipField.y = _skinBg.y + paddingTop;
			
			_skinBg.width = s.stageWidth-paddingLeft-paddingRight;
			tooltipField.width = _skinBg.width - paddingLeft-paddingRight;
			tooltipField.height = tooltipField.textHeight+4;
			_skinBg.height = tooltipField.height + paddingTop+paddingBottom;
			
		}
		
		
		private function removeToolTip():void
		{
			var s : Stage = StageManager.getInstance().stage;
			
			if( s.contains( tooltipField )  )
			{
				s.removeChild( tooltipField );
				s.removeChild( _skinBg );
				s.removeChild( _skinPointer );
			}
		}
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		protected function onToolTipOut(e:MouseEvent):void
		{
			tooltipField.removeEventListener(MouseEvent.ROLL_OUT, onToolTipOut );
			removeToolTip();
		}		
		
	}
}

class SingletonEnforcer{}
